﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace XLDL.Admin
{
    public partial class NhomSP : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                xuat_luoi();
            }
        }
        protected void xuat_luoi()
        {
            DataTable tb = new DataTable();
            tb = XLDL.XLDaTa("select * from Nhom_sanpham order by ngay_dang desc");
            Luoi.DataSource = tb;
            Luoi.DataBind();
            lbl_thongke.Text = "Tổng số Nhóm sản phẩm: " + tb.Rows.Count.ToString(); 
        }

        protected void Button_them_Click(object sender, EventArgs e)
        {
            DataTable tb = new DataTable();
            string ht = "";
            if(RadioButton_co.Checked==true)
            {
                ht="True";
            }
            else{ht="False";}
            string lenh="insert into nhom_sanpham(ten_nhom_sp,id_phu,hien_thi,ngay_dang) values(N'"+this.TextBox_tennhom.Text.Trim()+"',0,'"+ht+"','"+DateTime.Now.ToString("M/d/yyy HH:mm:ss")+"')";
            tb = XLDL.XLDaTa(lenh);
            xuat_luoi();
        }

        protected void Luoi_RowEditing(object sender, GridViewEditEventArgs e)
        {
            Luoi.EditIndex = e.NewEditIndex;
            xuat_luoi();
        }

        protected void Luoi_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            Luoi.EditIndex = -1;
            xuat_luoi();
        }

        protected void Luoi_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox ten = (TextBox)Luoi.Rows[e.RowIndex].Cells[1].FindControl("txt_tennhom");
            Label id = (Label)Luoi.Rows[e.RowIndex].Cells[0].FindControl("lbl_id");
            CheckBox ht=(CheckBox)Luoi.Rows[e.RowIndex].Cells[2].FindControl("chk_hienthi");
            string ck="";
            if(ht.Checked==true)
            {
                ck="True";
            }
            else{ck="False";}
            DataTable tb = new DataTable();
            string lenh = "update nhom_sanpham set ten_nhom_sp=N'"+ten.Text.Trim()+"', hien_thi='"+ck+"' where id_nhom_sp="+int.Parse(id.Text)+"";
            tb = XLDL.XLDaTa(lenh);

            Luoi.EditIndex = -1;
            xuat_luoi();
        }

        protected void Luoi_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Label id = (Label)Luoi.Rows[e.RowIndex].Cells[0].FindControl("lbl_id");
            DataTable tb = new DataTable();
            tb = XLDL.XLDaTa("delete from nhom_sanpham where id_nhom_sp=" + int.Parse(id.Text) + "");
            xuat_luoi();
        }

        protected void Button_reset_Click(object sender, EventArgs e)
        {
            this.TextBox_tennhom.Text = "";
            RadioButton_co.Checked = true;
        }
    }
}